package com.qidian.hw.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qidian.hw.pojo.RecruitInfos;
import com.qidian.hw.service.RecruitService;
import com.qidian.hw.service.impl.RecruitServiceImpl;
import com.qidian.hw.tools.Const;
import com.qidian.hw.tools.JSONTools;
import com.qidian.hw.tools.dto.Pager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author ������
 * @company �����
 * <p>
 * 2022��8��20�� ����1:51:49
 */
public class RecruitServlet extends HttpServlet {
    private RecruitService recruit = new RecruitServiceImpl ();

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getMethod ().equalsIgnoreCase ("POST")) {
            req.setCharacterEncoding ("utf-8");
        }
        String op = req.getParameter ("op");
        switch (op) {
            // ��������
            case "save":
                save (req, resp);
                break;
            // ��������
            case "input":
                input (req, resp);
                break;
            // �޸�����
            case "update":
                update (req, resp);
                break;
            // ��ʾ�����б�
            case "list":
                list (req, resp);
                break;
            // ɾ������
            case "delete":
                delete (req, resp);
                break;
            case "queryBypage":
                queryBypage (req, resp);
                break;
            case "deletes":
                deletes (req, resp);
                break;
            default:
                super.service (req, resp);
        }
    }

    private void deletes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String result = "0";
        String[] ids = req.getParameterValues ("recruitId");
        if (ids != null && ids.length > 0) {
            result = recruit.deletes (ids) + "";
        }
        resp.setContentType (Const.RES_PLAIN);
        resp.getWriter ().write (result);
    }

    private void queryBypage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        try {
            page = Integer.parseInt (req.getParameter ("page"));
        } catch (NumberFormatException e) {
        }
        int size = 10;
        try {
            size = Integer.parseInt (req.getParameter ("size"));
        } catch (NumberFormatException e) {
        }
        int id = 0;
        try {
            id = Integer.parseInt (req.getParameter ("recruitId"));
        } catch (NumberFormatException e) {
        }
        String title = req.getParameter ("title");
        Pager pager = recruit.queryByPage (page, size, title, id);
        resp.setContentType (Const.RES_JSON);
        resp.getWriter ().write (JSONTools.object2json (pager));
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long recruitId = 0L;
        String result = "0";
        try {
            recruitId = Long.parseLong (req.getParameter ("recruitId"));
        } catch (NumberFormatException e) {
            e.printStackTrace ();
        }
        if (recruitId != 0) {
            result = recruit.delete (recruitId) + "";
        }
        resp.setContentType (Const.RES_PLAIN);
        resp.getWriter ().write (result);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<RecruitInfos> list = recruit.queryAll ();
        String json = new ObjectMapper ().writeValueAsString (list);
        resp.setContentType ("application/json;charset=utf-8");
        resp.getWriter ().write (json);

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String recruitName = req.getParameter ("recruitName");
        String recruitDesc = req.getParameter ("recruitDesc");
        String recruitRequirement = req.getParameter ("recruitRequirement");
        String recruitImg = req.getParameter ("recruitImg");
        Long id = 0L;
        try {
            id = Long.parseLong (req.getParameter ("recruitId"));
        } catch (NumberFormatException e) {
        }
        RecruitInfos infos = new RecruitInfos ();
        infos.setRecruitName (recruitName);
        infos.setRecruitDesc (recruitDesc);
        infos.setRecruitRequirement (recruitRequirement);
        infos.setRecruitImg (recruitImg);
        infos.setRecruitId (id);
        String dateStr = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss").format (new Date ());
        infos.setModifyTime (dateStr);
        int result = recruit.update (infos);
        resp.setContentType (Const.RES_HTML);
        if (result == 1) {
            resp.getWriter ().write ("修改成功");
        } else resp.getWriter ().write ("修改失败");
    }

    private void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long recruitId = Long.parseLong (req.getParameter ("recruitId"));
        RecruitInfos infos = recruit.queryById (recruitId);
        String json = JSONTools.object2json (infos);
        resp.setContentType (Const.RES_JSON);
        resp.getWriter ().write (json);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RecruitInfos a2 = new RecruitInfos ();
        String recruitName = req.getParameter ("recruitName");
        String recruitDesc = req.getParameter ("recruitDesc");
        String recruitRequirement = req.getParameter ("recruitRequirement");
        String recruitImg = req.getParameter ("recruitImg");
        a2.setRecruitName (recruitName);
        a2.setRecruitDesc (recruitDesc);
        a2.setRecruitRequirement (recruitRequirement);
        a2.setRecruitImg (recruitImg);
        int result = recruit.save (a2);
        resp.setContentType ("text/plain;charset=utf-8");
        resp.getWriter ().write (result + "");
    }

}
